package com.mhkj.base.templete.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.mhkj.base.busiType.service.BusinessTypeService;
import com.mhkj.base.templete.entity.PlanTemplete;
import com.mhkj.base.templete.service.PlanTempleteDataService;
import com.mhkj.base.templete.service.PlanTempleteService;
import com.mhkj.common.enums.ApplyState;
import com.mhkj.common.enums.PlanTempleteType;
import com.mics.core.common.componet.JsonHandler;
import com.mics.core.common.componet.Pagination;
import com.mics.core.common.componet.ResponseMessage;
import com.mics.core.common.util.json.JsonUtils;
import com.mics.core.system.base.controller.BaseController;
import com.mics.core.system.base.service.FileService;
import com.mics.core.system.organizational.bean.Employee;
import com.mics.core.system.organizational.service.OrgService;
import com.mics.core.system.rbac.entity.Dictionary;
import com.mics.core.system.rbac.service.DictionaryService;

/**
 * 模板名称
 * @author mics
 * @date 2018年7月2日
 * @version 1.0
 */
@Controller
@RequestMapping("/planTemplete")
public class PlanTempleteController extends BaseController<PlanTemplete>{
	@Resource
	private PlanTempleteService planTempleteService;
	@Resource
	private FileService fileService;
	@Resource
	private OrgService orgService;
	@Resource
	private BusinessTypeService businessTypeService;
	@Resource
	private PlanTempleteDataService planTempleteDataService;
	@Resource
	private DictionaryService dictionaryService;
	@Resource
	private JsonHandler jsonHandler;
	@Resource
	public void setPlanTempleteService(PlanTempleteService planTempleteService) {
		super.setBaseService(planTempleteService);
	}
	
	@RequestMapping("/listByPage")
	@ResponseBody
	public void  listByPage(HttpServletResponse response,PlanTemplete planTemplete,int pageIndex, int pageSize){
		Pagination<PlanTemplete> planPagination = planTempleteService.listByPage(planTemplete, pageIndex,  pageSize);
		jsonHandler.sendJsonString(response, true, "获取数据成功", planPagination, new String[]{
				"id","name","type.value","professionalType.typeName","createUserName","createDate","state","remark"}, 
				"id","name","typeName","professionalType","createUserName","createDate","state","remark");
	}
	
	@RequestMapping("/save")
	@ResponseBody
	public ResponseMessage save(PlanTemplete planTemplete,MultipartFile file) {
		String message = planTempleteService.save(planTemplete,file);
		if("ok".equals(message)){
			return ResponseMessage.createMessage(true, "保存模板成功！");
		}else{
			return ResponseMessage.createMessage(false, message);
		}
	}

	@Override
	@RequestMapping("/edit")
	@ResponseBody
	public ResponseMessage edit(PlanTemplete planTemplete) {
		String message = planTempleteService.edit(planTemplete);
		if(message == null){
			return ResponseMessage.createMessage(true, "修改模板成功！");
		}else{
			return ResponseMessage.createMessage(false, message);
		}
	}

	@Override
	@RequestMapping("/del")
	@ResponseBody
	public ResponseMessage del(PlanTemplete t) {
		String message = planTempleteService.del(t);
		if("ok".equals(message)){
			return ResponseMessage.createMessage(true, "删除模板成功！");
		}else{
			return ResponseMessage.createMessage(false, message);
		}
		
	}

	@RequestMapping("/audit")
	@ResponseBody
	public ResponseMessage audit(long templeteId){
		PlanTemplete planTemplete = planTempleteService.getById(templeteId);
		planTemplete.setState(ApplyState.submit.getState());
		planTempleteService.edit(planTemplete);
		return ResponseMessage.createMessage(true, "提交审批成功");
	}
	
	@RequestMapping("/info")
	@ResponseBody
	public void info(HttpServletResponse response,long id) {
		PlanTemplete planTemplete =  super.info(id);
		Employee employee = orgService.employeeInfo(planTemplete.getCreateBy());
		planTemplete.setCreateUserName(employee.getName());
		jsonHandler.sendJsonString(response, true, "获取数据成功", planTemplete, new String[]{"id","name","type.value","remark"}, "id","name","type","remark");
	}
	
	
	@RequestMapping("/listKey")
	@ResponseBody
	public void listKey(HttpServletResponse response){
		Dictionary type = dictionaryService.getByCode(PlanTempleteType.KEY.getCode());
		List<PlanTemplete> planTempletes = planTempleteService.getByTempleteType(type.getId());
		JsonUtils.sendJsonFromList(response, true, "获取成功", planTempletes, new String[]{"id","name"}, "id","name");
	}
	
	@RequestMapping("/listMain")
	@ResponseBody
	public void listMain(HttpServletResponse response){
		Dictionary type = dictionaryService.getByCode(PlanTempleteType.MAIN.getCode());
		List<PlanTemplete> planTempletes = planTempleteService.getByTempleteType(type.getId());
		JsonUtils.sendJsonFromList(response, true, "获取成功", planTempletes,new String[]{"id","name"}, "id","name");
	}
	
	@RequestMapping("/listProfessional")
	@ResponseBody
	public void listProfessional(HttpServletResponse response){
		Dictionary type = dictionaryService.getByCode(PlanTempleteType.PROFESSIONAL.getCode());
		List<PlanTemplete> planTempletes = planTempleteService.getByTempleteType(type.getId());
		JsonUtils.sendJsonFromList(response, true, "获取成功", planTempletes,new String[]{"id","name"}, "id","name");
	}
	
	@RequestMapping("/listTempleteByPerfessionType")
	@ResponseBody
	public void listTempleteByPerfessionType(HttpServletResponse response,long perfessionTypeId){
		List<PlanTemplete> planTempletes = planTempleteService.getByPerfessionTypeId(perfessionTypeId);
		JsonUtils.sendJsonFromList(response, true, "获取成功", planTempletes,new String[]{"id","name"}, "id","name");
	}
	
	@RequestMapping("/listFunctionOffice")
	@ResponseBody
	public void listFunctionOffice(HttpServletResponse response){
		Dictionary type = dictionaryService.getByCode(PlanTempleteType.FUNCTION.getCode());
		List<PlanTemplete> planTempletes = planTempleteService.getByTempleteType(type.getId());
		JsonUtils.sendJsonFromList(response, true, "获取成功", planTempletes,new String[]{"id","name"}, "id","name");
	}
}
